"""
@File    :   models.PY
@Time    :   2024/03/14 10:20:14
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   双token相关数据模型
"""

from datetime import datetime

from sqlalchemy import JSON, BigInteger, Boolean, Column, DateTime, Index, String

from src.business.admin.system.oauth2.auth.enums import OAuth2GrantTypeEnum
from src.models.models import OrmBase

from .enums import TokenTypeEnum


class RefreshToken(OrmBase):
    """刷新令牌数据模型"""

    __tablename__ = "system_oauth2_refresh_token"
    __table_args__ = (Index(None, "refresh_token", "tenant_id"),)

    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    user_id = Column(BigInteger(), default=0, nullable=False, comment="用户ID")
    username = Column(String(20), default="", nullable=False, comment="用户名")
    user_is_admin = Column(
        Boolean(), default=False, nullable=False, comment="是否管理员用户"
    )
    expiration_datetime = Column(
        DateTime(), nullable=False, index=True, comment="过期时间"
    )
    oauth2_client_id = Column(
        BigInteger(), nullable=False, comment="对应OAuth2的客户端ID"
    )
    oauth2_grant_type = Column(
        String(50),
        default=OAuth2GrantTypeEnum.password.value,
        nullable=False,
        comment="OAuth2授权方式",
    )
    oauth2_scopes = Column(JSON(), nullable=False, default=[], comment="OAuth2授权范围")
    token_type = Column(
        String(50),
        default=TokenTypeEnum.bearer.value,
        nullable=False,
        comment="令牌类型(有的swagger需要)",
    )  # 一般保持默认值bearer即可
    # 租户ID(级联删除)
    tenant_id = Column(BigInteger(), nullable=False, index=True, comment="租户ID")
    tenant_is_system = Column(Boolean(), nullable=False, comment="是否系统级别租户")
    tenant_pakcage_id = Column(BigInteger(), nullable=False, comment="租户套餐ID")
    refresh_token = Column(String(255), nullable=False, unique=True, comment="刷新令牌")


class AccessToken(OrmBase):
    """访问令牌数据模型"""

    __tablename__ = "system_oauth2_access_token"
    __table_args__ = (Index(None, "refresh_token", "tenant_id"),)
    __table_args__ = (Index(None, "access_token", "tenant_id"),)

    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    user_id = Column(BigInteger(), default=0, nullable=False, comment="用户ID")
    username = Column(String(20), default="", nullable=False, comment="用户名")
    user_is_admin = Column(
        Boolean(), default=False, nullable=False, comment="是否管理员用户"
    )
    expiration_datetime = Column(
        DateTime(), nullable=False, index=True, comment="过期时间"
    )
    oauth2_client_id = Column(
        BigInteger(), nullable=False, comment="对应OAuth2的客户端ID"
    )
    oauth2_grant_type = Column(
        String(50),
        default=OAuth2GrantTypeEnum.password.value,
        nullable=False,
        comment="OAuth2授权方式",
    )
    oauth2_scopes = Column(JSON(), nullable=False, default=[], comment="OAuth2授权范围")
    token_type = Column(
        String(50),
        default=TokenTypeEnum.bearer.value,
        nullable=False,
        comment="令牌类型(有的swagger需要)",
    )  # 一般保持默认值bearer即可
    # 租户ID(级联删除)
    tenant_id = Column(BigInteger(), nullable=False, index=True, comment="租户ID")
    tenant_is_system = Column(Boolean(), nullable=False, comment="是否系统级别租户")
    tenant_pakcage_id = Column(BigInteger(), nullable=False, comment="租户套餐ID")
    access_token = Column(String(255), nullable=False, unique=True, comment="访问令牌")
    refresh_token = Column(
        String(255), nullable=False, index=True, comment="对应的刷新令牌"
    )
